package quanlydoanra.themmoi;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import quanlydoanra.sua.SuaDoanRa;
import quanlydoanra.utils.TableNguoiDi;
import quanlydoanra.utils.Utils;
import quanlydoanra.utils.managerID;
import quanlydoanra.utils.truyXuatDatabase;
import utils.DateUtils;
import utils.UtilsOnScopes;
import utils.Validate;

import dbutil.ConnectDB;
import dbutil.DBUtils;
public class DoanRa {
	private String maDoan = null;
	private String nuocDen = null;
	private String loaiDoan = null;
	private String linhVuc = null;
	private String mucDich = null;
	private java.util.Date ngayDi = null;
	private java.util.Date ngayVe = null;
	private String kinhPhi = null;
	private boolean baoCao = false;
	private String donViMoi = null;
	private String soQuyetDinh = null;
	private java.util.Date ngayKyQuyetDinh = null;
	private String nguoiKyQuyetDinh = null;
	private SelectItem[] listMucDich = null;
	private List<TableNguoiDi> tablesNguoiDi = null;	
	private String error = null;
	private List<SuaDoanRa> dsDoanRa = null;
	private boolean display;
	
	public DoanRa(){
	}
	public boolean isDisplay() {
		return display;
	}
	public void setDisplay(boolean display) {
		this.display = display;
	}
	public DoanRa( String nuocDen, String loaiDoan, String linhVuc,
			String mucDich, java.util.Date ngayDi, java.util.Date ngayVe, String kinhPhi,
			boolean baoCao, String donViMoi,
			String soQuyetDinh, java.util.Date ngayKyQuyetDinh, String nguoiKyQuyetDinh) {
		this.nuocDen = nuocDen;
		this.loaiDoan = loaiDoan;
		this.linhVuc = linhVuc;
		this.mucDich = mucDich;
		this.ngayDi = ngayDi;
		this.ngayVe = ngayVe;
		this.kinhPhi = kinhPhi;
		this.baoCao = baoCao;
		this.donViMoi = donViMoi;
		this.soQuyetDinh = soQuyetDinh;
		this.ngayKyQuyetDinh = ngayKyQuyetDinh;
		this.nguoiKyQuyetDinh = nguoiKyQuyetDinh;
	}

	public String getMaDoan() {
		return maDoan;
	}


	public void setMaDoan(String maDoan) {
		this.maDoan = maDoan;
	}


	public void setBaoCao(boolean baoCao) {
		this.baoCao = baoCao;
	}


	public String getNuocDen() {
		return nuocDen;
	}

	public void setNuocDen(String nuocDen) {
		this.nuocDen = nuocDen;
	}

	public String getLoaiDoan() {
		return loaiDoan;
	}

	public void setLoaiDoan(String loaiDoan) {
		this.loaiDoan = loaiDoan;
	}

	public String getLinhVuc() {
		return linhVuc;
	}

	public void setLinhVuc(String linhVuc) {
		this.linhVuc = linhVuc;
	}

	public String getMucDich() {
		return mucDich;
	}

	public void setMucDich(String mucDich) {
		this.mucDich = mucDich;
	}

	public java.util.Date getNgayDi() {
		return ngayDi;
	}

	public void setNgayDi(java.util.Date ngayDi) {
		this.ngayDi = ngayDi;
	}

	public java.util.Date getNgayVe() {
		return ngayVe;
	}

	public void setNgayVe(java.util.Date ngayVe) {
		this.ngayVe = ngayVe;
	}

	public String getKinhPhi() {
		return kinhPhi;
	}

	public void setKinhPhi(String kinhPhi) {
		this.kinhPhi = kinhPhi;
	}

	public boolean isBaoCao() {
		return baoCao;
	}

	public void isBaoCao(boolean baoCao) {
		this.baoCao = baoCao;
	}

	public String getDonViMoi() {
		return donViMoi;
	}

	public void setDonViMoi(String donViMoi) {
		this.donViMoi = donViMoi;
	}

	public String getSoQuyetDinh() {
		return soQuyetDinh;
	}

	public void setSoQuyetDinh(String soQuyetDinh) {
		this.soQuyetDinh = soQuyetDinh;
	}

	public java.util.Date getNgayKyQuyetDinh() {
		return ngayKyQuyetDinh;
	}

	public void setNgayKyQuyetDinh(java.util.Date ngayKyQuyetDinh) {
		this.ngayKyQuyetDinh = ngayKyQuyetDinh;
	}

	public String getNguoiKyQuyetDinh() {
		return nguoiKyQuyetDinh;
	}

	public void setNguoiKyQuyetDinh(String nguoiKyQuyetDinh) {
		this.nguoiKyQuyetDinh = nguoiKyQuyetDinh;
	}
	
	public void setTablesNguoiDi(List<TableNguoiDi> tablesNguoiDiNguoiDi) {
		this.tablesNguoiDi = tablesNguoiDi;
	}


	public String getError() {	
		return error;
	}
	public void setError(String error) {
		this.error = error;
	}
	public SelectItem[] getListMucDich() {
		if (listMucDich==null){
			this.listMucDich = loadFirstItem();
		}
		return this.listMucDich;
	}

	public void setListMucDich(SelectItem[] listItemMucDich) {
		this.listMucDich = listItemMucDich;
	}	
	/**************************************
	 * Method name		: resetData
	 * Return type		: void
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public void resetData(){
		this.maDoan = null;
		this.nuocDen = "";
		this.loaiDoan = "";
		this.linhVuc = "";
		this.mucDich = "";
		this.ngayDi = null;
		this.ngayVe = null;
		this.kinhPhi = "";
		this.baoCao = false;
		this.donViMoi = "";
		this.soQuyetDinh = "";
		this.ngayKyQuyetDinh = null;
		this.nguoiKyQuyetDinh = "";
		this.tablesNguoiDi = null;
			
	}
	int intPhanBienThemSuaDoanRa = 0;
//****************** Them moi doan ra ***********************
	
	/**************************************
	 * Method name		: insertDoanRa
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String insertDoanRa(){
		this.maDoan = null;
		this.setError("");
		if (this.checkInputValue() && this.kiemTraNgayDi() == true){
		HttpSession ses = UtilsOnScopes.getSession();
	
		//lay ra ma muc dich
		try{
			//Lay id cua muc dich
			MucDich mucDich = new MucDich(this.linhVuc,this.mucDich);
			int idMucDich = mucDich.timLinhVuc();
			
			//Lay id quyet dinh
			QuyetDinh quyetDinh = new QuyetDinh(this.soQuyetDinh,this.ngayKyQuyetDinh,this.nguoiKyQuyetDinh);
			int idQuyetDinh = quyetDinh.insertQuyetDinh();
			String madoan = managerID.getNewID("doanra");
			String sql1 = "insert into doanra(madoan,nuocden,loaidoan,ngaydi,ngayve,donvimoi,baocaokq," +
				"idmucdich,chiphi,idquyetdinh,trangthai,flagdelete) values('"+madoan+"','"+this.nuocDen+"','"+this.loaiDoan+"','"+DateUtils.utilDateToSqlDate(this.ngayDi)+"','"+DateUtils.utilDateToSqlDate(this.ngayVe)+"','"+this.donViMoi+"',"+this.baoCao+","+idMucDich+",'"+this.kinhPhi+"',"+idQuyetDinh+",false,0)";
	
		int acc = truyXuatDatabase.insertData(sql1);
		if(acc == 1){
			this.maDoan = madoan;			
			ses.setAttribute("maDoanRa", this.maDoan);
			this.setDisplay(true);
			error = "Đoàn đã được thêm với mã là "+this.maDoan;
			this.setError(error);
		}
		else  {
			this.maDoan = null;
			error ="Việc thêm đoàn ra đã xảy ra sự cố.Yêu cầu nhập lại thông tin";
			this.setError(error);
		}
		}
		catch(Exception ex){
			System.out.println("Loi");
		}
		}
		else {
			String er = this.getError();
			this.setError(er);			
		} 
		return "success";
		
	}
	/**************************************
	 * Method name		: huyInsert
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String huyInsert(){
	this.resetData();
	this.maDoan =null;
	this.setError("");
	this.setDisplay(false);
	HttpSession ses = UtilsOnScopes.getSession();
	ses.removeAttribute("maDoanRa");
	return "huy";
	}
	/**************************************
	 * Method name		: distincMucDich
	 * Return type		: boolean
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public boolean distincMucDich(List<MucDich> listMucDich,MucDich m){
		boolean result = true;
		for (MucDich l : listMucDich) {
			if(l.getMucdich().equals(m.getMucdich())){
				result = false;
				break;
			}			
		}
		return result;
	}
	/**************************************
	 * Method name		: dsMucDich
	 * Return type		: List<MucDich>
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public  List<MucDich> dsMucDich(){
		List<MucDich> listMucDich = new ArrayList<MucDich>();
		try{
		String SQL = "SELECT DISTINCT  id,mucdich FROM mucdich where linhvuc ='"+this.linhVuc+"'" ;
		ArrayList array = ConnectDB.getDataTypeList(SQL);
			if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			for (int i = 0; i < array.size(); i++) {
				MucDich MD = new MucDich();
				hash = (HashMap<String, Object>) array.get(i);
				MD.setId(Integer.parseInt(hash.get("id").toString()));
				MD.setMucdich(hash.get("mucdich").toString());
				if(this.distincMucDich(listMucDich,MD) == true)
					listMucDich.add(MD);
			}
			System.out.println("SQL muc dich "+SQL);
		}
		}
		catch(Exception ex){
			System.out.println("Loi");
		}
		return listMucDich;
	}
	
	/**************************************
	 * Method name		: listItemMucDich
	 * Return type		: SelectItem[]
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public SelectItem[] listItemMucDich() {
		List<MucDich> listFunct = this.dsMucDich();
		System.out.println("Vao list item muc dich");
		SelectItem[] arrItem = new SelectItem[listFunct.size()];
		for (int i = 0; i < listFunct.size(); i++) {
			MucDich FD = listFunct.get(i);
			arrItem[i] = new SelectItem(FD.getMucdich(), FD.getMucdich());
		}
		return arrItem;
	}
	/**************************************
	 * Method name		: loadFirstItem
	 * Return type		: SelectItem[]
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public SelectItem[] loadFirstItem(){
		List<MucDich> listMucDich = new ArrayList<MucDich>();
		try{
		String SQL = "SELECT DISTINCT  id,mucdich FROM mucdich where linhvuc ='Kinh tế'" ;
		ArrayList array = ConnectDB.getDataTypeList(SQL);
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			for (int i = 0; i < array.size(); i++) {
				MucDich MD = new MucDich();
				hash = (HashMap<String, Object>) array.get(i);
				MD.setId(Integer.parseInt(hash.get("id").toString()));
				MD.setMucdich(hash.get("mucdich").toString());
				if(this.distincMucDich(listMucDich,MD) == true)
					listMucDich.add(MD);
			}
		}
		}
		catch(Exception ex){
			System.out.println("Loi");
		}
	
		SelectItem[] arrItem = new SelectItem[listMucDich.size()];
		for (int i = 0; i < listMucDich.size(); i++) {
			MucDich FD = listMucDich.get(i);
			arrItem[i] = new SelectItem(FD.getMucdich(), FD.getMucdich());
		}
		return arrItem;
	}
	/**************************************
	 * Method name		: locTheoLinhVuc
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String locTheoLinhVuc(){
		SelectItem[] select = this.listItemMucDich();
		this.setListMucDich(select);
		return "finish_them";
	}
	
	/**************************************
	 * Method name		: locTheoLinhVucSua
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String locTheoLinhVucSua(){
		SelectItem[] select = this.listItemMucDich();
		System.out.println("Loc theo linh vuc");
		this.setListMucDich(select);
		return "finish_sua";
	}
	
	//Load ds thanh vien tuong ung cua doan ra

	/**************************************
	 * Method name		: getTablesNguoiDi
	 * Return type		: List<TableNguoiDi>
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public List<TableNguoiDi> getTablesNguoiDi() {		
		List<TableNguoiDi> list = new ArrayList<TableNguoiDi>();
		HttpSession ses = UtilsOnScopes.getSession();
		String mD =  (String) ses.getAttribute("maDoanRa");
		String SQL = "select * from dsnguoidi as DS, canhan as CN where DS.iddoanra ='"+mD+"' and CN.cmnd = DS.cmnd ORDER BY tennguoidi";
		ArrayList<HashMap<String, Object>> array = ConnectDB.getDataTypeList(SQL);
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			int stt = 0;
			for (int i = 0; i < array.size(); i++) {
				stt = i + 1;
				hash = (HashMap<String, Object>) array.get(i);
				TableNguoiDi table = new TableNguoiDi();
				table.setStt(String.valueOf(stt));
				table.setId(hash.get("id").toString());
				table.setTenNguoiDi(hash.get("tennguoidi").toString());
				table.setSoHC(hash.get("sohc").toString());
				table.setCmnd(hash.get("cmnd").toString());
				table.setCoQuan(hash.get("coquan").toString());
				String gioitinh ="";
				boolean b = Boolean.parseBoolean(hash.get("gioitinh").toString());
				if (b == true) gioitinh = "Nam";
				else gioitinh = "Nữ";
				table.setGioiTinh(gioitinh);
				table.setNamSinh(hash.get("namsinh").toString());
				table.setTrangThaiDi(Boolean.parseBoolean(hash.get("trangthaidi").toString()));
				table.setChucVu(hash.get("chucvu").toString());
				list.add(table);
			}
		}
		this.tablesNguoiDi = list;
		return this.tablesNguoiDi;
		
	}
	public boolean kiemTraNgayDi(){
		boolean result = false;
		if(Utils.kiemTraNgay(new java.util.Date(), this.ngayDi) == false){
			this.setError("Ngày đi phải sau ngày hiện tại! ");
			result = false;
		}
		else {
			this.setError("");
			result = true;
		}
		return result;
	}
	/**************************************
	 * Method name		: checkInputValue
	 * Return type		: boolean
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public boolean checkInputValue(){
		boolean result = false;
		String str ;
		if(this.loaiDoan == null || this.loaiDoan.equals("") ||
				this.nuocDen == null || this.nuocDen.equals("") || 
				this.ngayDi == null || this.ngayDi.equals("") || 
				this.ngayVe == null || this.ngayVe.equals("") ||
				this.soQuyetDinh == null || this.soQuyetDinh.equals("")||
				this.ngayKyQuyetDinh == null || this.ngayKyQuyetDinh.equals("")||
				this.nguoiKyQuyetDinh == null || this.nguoiKyQuyetDinh.equals("")||
				this.donViMoi == null || this.donViMoi.equals("")){
		result = false;
		this.setError("Nhập đầy đủ thông tin có dấu (*) phía trước");
			}
		
		else if(Utils.kiemTraNgay(this.ngayDi, this.ngayVe) == false){
			this.setError("Ngày đi phải trước ngày về! ");
			result = false;
			
		}
		else if(Utils.kiemTraNgay(this.ngayKyQuyetDinh, this.ngayDi) == false){
			this.setError("Ngày ký quyết định phải trước ngày đi! ");
			result = false;
		}
		else {
			this.setError("");
			result = true;
		}
		return result;
	}
	int intPhanBietThemSuaDoanRa =0;
	public String themThanhVien(){
		if(this.maDoan == null) {
			this.setError("Phải tạo đoàn ra trước khi tạo thành viên");
			return "success";
		}
		else {
		this.error ="";
		intPhanBietThemSuaDoanRa = 100;
		HttpSession ses = UtilsOnScopes.getSession();
		ses.setAttribute("phanbiet",intPhanBietThemSuaDoanRa);
		return "themthanhvien";
		}
	}
	
	//************** Ket thuc them moi doan ra *******************
	
	//******************************* Sua doan ra************************
	
	int acc =1;
	public List<SuaDoanRa> getDsDoanRa() {	
		System.out.println("vao ds doan ra");
			acc =1;
			this.helpTimKiem();
			return dsDoanRa;
	}
	public void setDsDoanRa(List<SuaDoanRa> dsDoanRa) {
		this.dsDoanRa = dsDoanRa;
	}
	String accMaDoan ="";
	
	/**************************************
	 * Method name		: helpTimKiem
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String helpTimKiem(){
		accMaDoan = this.maDoan;
		String result = "success_suadoanra";		
		this.setError("");
		List<SuaDoanRa> list = new ArrayList<SuaDoanRa>();
		String SQL ="";
			if(acc == 0){
				SQL ="select * from doanra as DR, mucdich as MD, quyetdinh as QD where QD.id = DR.idquyetdinh and MD.id = DR.idmucdich and TRIM(LOWER(madoan)) LIKE TRIM(LOWER('%"+this.maDoan+"%')) and flagdelete = 0 ORDER BY madoan DESC";
			}
			else if(acc ==1){
				SQL = "select * from doanra as DR, mucdich as MD, quyetdinh as QD where QD.id = DR.idquyetdinh and MD.id = DR.idmucdich and flagdelete = 0 ORDER BY madoan DESC";
			}
			else if(acc ==2) {
				String strMaDoan ="";
				String strLoaiDoan = "";
				String strLinhVuc = "";
				String strNuocDen = "";
				String strMucDich ="";
				String strNguoiKyQD = "";
				String strChiPhi = "";
				String strDonViMoi = "";
				String strSoQD ="";
				String strDateNgayDi = "";
				String strDateNgayVe = "";
				String strDateNgayQuyDinhNop ="";
				String strDateNgayKyQD = "";
				if(this.maDoan != null){
					if(this.maDoan.length() > 0)
					strMaDoan ="%"+this.maDoan+"%";
					else strMaDoan = "%";
				}
				else strMaDoan = "%";
				if(this.loaiDoan != null){
					if(this.loaiDoan.length() > 0)
					strLoaiDoan = "%"+this.loaiDoan+"%";
					else strLoaiDoan ="%";
				}
				else strLoaiDoan ="%";
			
				if(this.nuocDen != null) {
					if(this.nuocDen.length() > 0)
					strNuocDen = "%"+this.nuocDen+"%";
					else strNuocDen = "%";
				}
				else strNuocDen = "%";				
				strLinhVuc = "%"+this.linhVuc+"%";				
				strMucDich = "%"+this.mucDich+"%";				
				if(this.ngayDi != null)  {
					if(!this.ngayDi.equals(""))
					strDateNgayDi = "DR.ngaydi = '"+this.ngayDi+"' and ";
				}
				if(this.ngayVe != null) {
					if(!this.ngayVe.equals(""))
					strDateNgayVe  = "DR.ngayve = '"+this.ngayVe+"' and ";
				}
				if(this.soQuyetDinh != null) {
					if(this.soQuyetDinh.length() > 0)
					strSoQD = "%"+this.soQuyetDinh+"%";
					else strSoQD = "%";
				}
				else strSoQD = "%";
				if(this.ngayKyQuyetDinh != null) {
					if(!this.ngayKyQuyetDinh.equals(""))
					strDateNgayKyQD ="QD.ngaykyqd = '"+this.ngayKyQuyetDinh+"' and ";
				}
				if(this.nguoiKyQuyetDinh != null) {
					if(this.nguoiKyQuyetDinh.length() > 0)
					strNguoiKyQD = "%"+this.nguoiKyQuyetDinh+"%";
					else strNguoiKyQD = "%";
				}
				else strNguoiKyQD = "%";
				if(this.kinhPhi != null) {
					if(this.kinhPhi.length() > 0)
					strChiPhi = "%"+this.kinhPhi+"%";
					else strChiPhi = "%";
				}
				else strChiPhi = "%";
				if(this.donViMoi != null) {
					if(this.donViMoi.length() > 0)
					strDonViMoi = "%"+this.donViMoi+"%";
					else strDonViMoi = "%";
				}
				else strDonViMoi = "%";
				SQL ="select * from doanra as DR, mucdich as MD, quyetdinh as QD " +
				"where QD.id = DR.idquyetdinh and " +
				"MD.id = DR.idmucdich and " +
				"madoan like '"+strMaDoan+"' and " +
				"DR.loaidoan like '"+strLoaiDoan+"' and " +
				"DR.nuocden like '"+strNuocDen+"' and  "+
				"MD.linhvuc like '"+strLinhVuc+"' and "+
				"MD.mucdich like '"+strMucDich+"' and "+
				strDateNgayDi +
				strDateNgayVe+
				strDateNgayQuyDinhNop+
				"QD.soqd like '"+strSoQD+"' and "+
				strDateNgayKyQD+
				"QD.nguoikyqd like '"+strNguoiKyQD+"' and "+
				"DR.chiphi like '"+strChiPhi+"' and "+
				"DR.donvimoi like '"+strDonViMoi+"'"+
				" and flagdelete = 0";
			}
			else if(acc == 3){
				HttpServletRequest request = UtilsOnScopes.getRequest();
				String lnkMaDoan = request.getParameter("lnkMaDoan");
				SQL ="select * from doanra as DR, mucdich as MD, quyetdinh as QD where QD.id = DR.idquyetdinh and MD.id = DR.idmucdich and madoan = '"+lnkMaDoan+"' and flagdelete = 0";
			}
		System.out.println("SQL "+SQL);
		ArrayList<HashMap<String, Object>> array = ConnectDB.getDataTypeList(SQL);
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			int stt = 0;
			for (int i = 0; i < array.size(); i++) {
				
				//***********8 Dua thong tin vao danh sach doan ra o phia duoi
				stt = i + 1;
				hash = (HashMap<String, Object>) array.get(i);
				SuaDoanRa doanRa = new SuaDoanRa();
				String loaidoanra = hash.get("loaidoan").toString();
				String madoanra = hash.get("madoan").toString();
				String sqlNgayDi = hash.get("ngaydi").toString();
				String sqlNgayVe = hash.get("ngayve").toString();
				String strNgayDi = DateUtils.formatDate(sqlNgayDi);
				String strNgayVe = DateUtils.formatDate(sqlNgayVe);
				String nuocden = hash.get("nuocden").toString();
				String donvimoi =hash.get("donvimoi").toString();
				doanRa.setStt(String.valueOf(stt));
				doanRa.setMaDoanRa(madoanra);
				doanRa.setLoaiDoanRa(loaidoanra);				
				doanRa.setNgayDi(strNgayDi);				
				doanRa.setNgayVe(strNgayVe);
				doanRa.setNuocDen(nuocden);
				doanRa.setDonViMoi(donvimoi);
				
				//***************** Ket thuc dua thong tin ***************
				
				//******************* set thong tin len textbox
				if(array.size() == 1){
				this.setMaDoan(madoanra);	
				accMaDoan = madoanra;
				this.setLoaiDoan(loaidoanra);
				this.setNuocDen(nuocden);
				this.setLinhVuc(hash.get("linhvuc").toString());
				this.setMucDich(hash.get("mucdich").toString());
				this.setNgayDi(DateUtils.sqlDateToutilDate(java.sql.Date.valueOf(sqlNgayDi)));
				this.setNgayVe(DateUtils.sqlDateToutilDate(java.sql.Date.valueOf(sqlNgayVe)));
				this.setSoQuyetDinh(hash.get("soqd").toString());
				this.setNgayKyQuyetDinh(DateUtils.sqlDateToutilDate(java.sql.Date.valueOf(hash.get("ngaykyqd").toString())));
				this.kinhPhi = hash.get("chiphi").toString();
				System.out.println("Kinh phi "+this.kinhPhi);
				this.setBaoCao(Boolean.parseBoolean(hash.get("baocaokq").toString()));
				this.setNguoiKyQuyetDinh(hash.get("nguoikyqd").toString());
				this.setDonViMoi(donvimoi);
				}
				else {
					this.maDoan =null;
					this.loaiDoan =null;
					this.nuocDen =null;
					this.ngayDi =null;
					this.ngayVe = null;
					this.soQuyetDinh =null;
					this.ngayKyQuyetDinh =null;
					this.kinhPhi =null;
					this.donViMoi =null;
					this.baoCao =false;
					this.nguoiKyQuyetDinh =null;
				}
				//************* Ket thuc set thong tin len textbox
				
				list.add(doanRa);
			}
		}
		else {
			if(this.maDoan != null)
			this.setError("Không tìm thấy đoàn có mã số đã nhập");
			else {
				this.maDoan = null;
				this.setError("Không tìm thấy đoàn với thông tin đã nhập");
			}
		}
		this.setDsDoanRa(list);
		return result;
	}
	/**************************************
	 * Method name		: displayAll
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String displayAll(){
		acc =1;
		this.resetData();
		this.maDoan =null;
		this.tablesNguoiDi = null;
		this.helpTimKiem();
		return "success_suadoanra";
	}
	/**************************************
	 * Method name		: timKiemTheoMa
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String timKiemTheoMa(){
		acc =0;
		this.helpTimKiem();
		return "success_suadoanra";
	}
	/**************************************
	 * Method name		: timKiemTheoLink
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String timKiemTheoLink(){
		acc =3;
		this.helpTimKiem();
		return "success_suadoanra";
	}
	
	//tim kiem mo rong
	/**************************************
	 * Method name		: timKiemMoRong
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String timKiemMoRong(){
		acc =2;
		this.helpTimKiem();
		return "success_suadoanra";
	}
	
	/**************************************
	 * Method name		: capNhatDoanRa
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String capNhatDoanRa(){
		String userName = UtilsOnScopes.getUserName();
	if(this.maDoan != null){
	if(this.checkInputValue()){
		//neu la admin thi ko can goi yeu cau
	if(!userName.equals("admin")){
		if(truyXuatDatabase.getDuyet(this.maDoan) == -1) {
			this.setError("Bạn phải gửi yêu cầu đến người quản trị trước khi sửa đoàn ra này!");
		}
		else if(truyXuatDatabase.getDuyet(this.maDoan) == 0) {
			this.setError("Yêu cầu của bạn đang ở trạng thái chờ duyệt");
		}		
		else{
			if(!accMaDoan.equals(this.maDoan)){
				this.setError("Không được thay đổi mã đoàn");
		}
			else {
				if(this.checkInputValue()){
				if(truyXuatDatabase.capNhatDoanRa(this.maDoan, this.loaiDoan, this.baoCao, this.nuocDen, this.linhVuc, this.mucDich, DateUtils.utilDateToSqlDate(this.ngayDi), DateUtils.utilDateToSqlDate(this.ngayVe), this.soQuyetDinh, DateUtils.utilDateToSqlDate(this.ngayKyQuyetDinh), this.nguoiKyQuyetDinh, this.kinhPhi, this.donViMoi)){
				this.setError("Đoàn ra có mã "+this.maDoan+" đã được cập nhật thành công");
				truyXuatDatabase.insertNhatKyXoaSua(DBUtils.getIDThanhVien()+"", DateUtils.utilDateToSqlDate(new Date()), this.maDoan, "Sửa", "Sửa đối tượng này trong cssdl");
			}
				else this.setError("Quá trình cập nhật xãy ra lỗi");
			}
			}
		}
	}
	else {
		if(!accMaDoan.equals(this.maDoan)){
			this.setError("Không được thay đổi mã đoàn");
	}
		else {
			if(truyXuatDatabase.capNhatDoanRa(this.maDoan, this.loaiDoan, this.baoCao, this.nuocDen, this.linhVuc, this.mucDich, DateUtils.utilDateToSqlDate(this.ngayDi), DateUtils.utilDateToSqlDate(this.ngayVe), this.soQuyetDinh, DateUtils.utilDateToSqlDate(this.ngayKyQuyetDinh), this.nguoiKyQuyetDinh, this.kinhPhi, this.donViMoi)){
			this.setError("Đoàn ra có mã "+this.maDoan+" đã được cập nhật thành công");
			truyXuatDatabase.insertNhatKyXoaSua(DBUtils.getIDThanhVien()+"", DateUtils.utilDateToSqlDate(new Date()), this.maDoan, "Sửa", "Sửa đối tượng này trong cssdl");
		}
			else this.setError("Quá trình cập nhật xãy ra lỗi");
		}
	}
	}
	}else {
		this.setError("Bạn phải nhập mã đoàn");
	}
		return "success_suadoanra";
		
	}
	/**************************************
	 * Method name		: capNhatThanhVien
	 * Return type		: String
	 * Description		:
	 * Created date		: Aug 12, 2008
	 * Author			: Ngoc Diem
	 **************************************/
	public String capNhatThanhVien(){
		String userName = UtilsOnScopes.getUserName();
		
		if(!userName.equals("admin")){
			if(truyXuatDatabase.getDuyet(this.maDoan) == -1) {
				this.setError("Bạn phải gửi yêu cầu đến người quản trị trước khi sửa đoàn ra này!");
				return "fail_suadoanra_capnhatthanhvien";
			}
			else if(truyXuatDatabase.getDuyet(this.maDoan) == 0) {
				this.setError("Yêu cầu của bạn đang ở trạng thái chờ duyệt");
				return "fail_suadoanra_capnhatthanhvien";
				}
			else{
				this.error = "";
				intPhanBietThemSuaDoanRa = 200;		
				HttpServletRequest request = UtilsOnScopes.getRequest();
				String maDoan_TV = request.getParameter("maDoan_TV");
				HttpSession ses = UtilsOnScopes.getSession();
				ses.setAttribute("maDoanRa",maDoan_TV);
				ses.setAttribute("phanbiet",intPhanBietThemSuaDoanRa);
				return "success_suadoanra_capnhatthanhvien";
			}
		}
			else {
				this.error = "";
				intPhanBietThemSuaDoanRa = 200;		
				HttpServletRequest request = UtilsOnScopes.getRequest();
				String maDoan_TV = request.getParameter("maDoan_TV");
				HttpSession ses = UtilsOnScopes.getSession();
				ses.setAttribute("maDoanRa",maDoan_TV);
				ses.setAttribute("phanbiet",intPhanBietThemSuaDoanRa);
				return "success_suadoanra_capnhatthanhvien";
			}
		
	}
	public String guiYeuCau(){
		return "dentrangguiyeucau_doanra";
	}
	

}
